/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.db4ofiletype;

import com.db4o.nb.api.Db4oProvider;
import com.db4o.nb.api.Db4oServer;
import com.db4o.nb.api.impl.Db4oProviderImpl;
import java.beans.PropertyChangeEvent;
import java.io.IOException;
import java.util.logging.Logger;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObjectExistsException;
import org.openide.loaders.MultiDataObject;
import org.openide.nodes.CookieSet;
import org.openide.nodes.Node;
import org.openide.nodes.NodeAdapter;
import org.openide.nodes.NodeEvent;
import org.openide.nodes.NodeListener;
import org.openide.nodes.NodeMemberEvent;
import org.openide.nodes.NodeReorderEvent;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 * Net Beans API: Data Object for the db4o netbeans plug in.
 */
public class Db4oDataObject extends MultiDataObject implements Lookup.Provider {
  
  private static final Logger ERR = Logger.getLogger(Db4oDataObject.class.getName());
  
  final InstanceContent ic;
  private AbstractLookup lookup;
  
  /**
   * Creates an instance of the data object.
   */
  public Db4oDataObject(FileObject pf, Db4oDataLoader loader) throws DataObjectExistsException, IOException {
    super(pf, loader);

    
    Db4oProvider db4oProvider = new Db4oProviderImpl(this);
    
    CookieSet cookies = getCookieSet();
    cookies.add( (Node.Cookie) Db4oEditorSupport.create(this, getPrimaryEntry(), cookies));
    cookies.add( (Node.Cookie) db4oProvider );

//    cookies.add((Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), cookies));

    this.ic = new InstanceContent();
    this.lookup = new AbstractLookup(ic);
    this.ic.add(Db4oEditorSupport.create(this, this.getPrimaryEntry(), cookies));
    this.ic.add(db4oProvider);
    this.ic.add(this);
    
  }
  
  protected Node createNodeDelegate() {
    
    Db4oProvider db4o = (Db4oProvider)this.getCookie(Db4oProvider.class);
    if( db4o != null ) {
 
      Db4oDatabaseNode n = new Db4oDatabaseNode(this, db4o.getDatabase());      
//      n.addNodeListener( new NodeAdapter() {
//        public void nodeDestroyed(NodeEvent nodeEvent) {
//          Db4oDatabaseNode dbNode = (Db4oDatabaseNode)nodeEvent.getNode();
//          Db4oProvider db4oProvider = (Db4oProvider)Db4oDataObject.this.getCookie(Db4oProvider.class);
//          if( db4oProvider != null ) {
//            Db4oServer server = db4oProvider.getServerHost();
//            if( server != null) {
//              server.stop();
//            }
//          }
//        }        
//      });
      return n;
      
    } else {
      return null;
    }
    
  }
  
  public Lookup getLookup() {
    return lookup;
  }
  
  public Node.Cookie getCookie(Class type) {
    Object o = lookup.lookup(type);
    return (o instanceof Node.Cookie)? (Node.Cookie)o : null;
  }

  public void setModified(boolean b) {
    super.setModified(b);
    
    SaveCookie save = (SaveCookie)getLookup().lookup(SaveCookie.class);
    
    if( b ) {
      if( save == null ) {
        this.ic.add( new SaveCookie() {
          public void save() throws IOException {
              Db4oProvider db4o = (Db4oProvider)Db4oDataObject.this.getCookie(Db4oProvider.class);
              if( db4o != null ) {
                db4o.save();
              }
              Db4oDataObject.this.setModified(false);
          }
        });
      }
    } else {
      if( save != null ) {
        this.ic.remove(save);
      }
      
    }
  }
  

}
